<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索结果</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .navbar {
            background-color: #ff5000;
            color: white;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .navbar .logo {
            display: flex;
            align-items: center;
        }
        .navbar .logo span {
            margin-left: 10px;
        }
        .navbar a {
            color: white;
            text-decoration: none;
            margin: 0 10px;
        }
        .navbar button {
            background-color: white;
            color: #ff5000;
            border: none;
            padding: 5px 10px;
            margin-left: 10px;
            border-radius: 5px;
            cursor: pointer;
        }
        .search-results {
            margin: 20px;
            padding: 0 20px;
        }
        .search-results h2 {
            font-size: 24px;
            margin-bottom: 20px;
            border-bottom: 2px solid #ff5000;
            display: inline-block;
        }
        .product-list {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }
        .product-item {
            width: 200px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin: 20px;
            text-align: center;
            padding: 10px;
        }
        .product-item img {
            width: 100%;
            height: 150px;
            object-fit: cover;
            border-bottom: 1px solid #ccc;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div class="navbar">
    <div class="logo">机械工业</div>
    <div class="nav-links">
        <a id="index1Link" href="#">首页</a>
        <a id="userMessageLink" href="#">我的</a>
        <a id="cartLink" href="#">购物车</a>
    </div>
</div>

<div class="search-results">
    <h2>搜索结果</h2>
    <div class="product-list" id="searchResultsList">
        <!-- 搜索结果列表将通过 JavaScript 动态生成 -->
    </div>
</div>

<script>
    function getUserIdFromUrl() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('userId');
    }

    const userId = getUserIdFromUrl();
    const userMessageLink = document.getElementById("userMessageLink");
    const index1Link = document.getElementById("index1Link");
    const cartLink = document.getElementById("cartLink");

    userMessageLink.href = `userMessage?id=${userId}`;
    index1Link.href = `index1?id=${userId}`;
    cartLink.href = `cart?id=${userId}`;

    window.onload = function() {
        var urlParams = new URLSearchParams(window.location.search);
        var keyword = urlParams.get('keyword');

        if (keyword) {
            // 发送请求到服务器，获取搜索结果，并展示在页面上
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/api/search?keyword=' + encodeURIComponent(keyword), true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var products = JSON.parse(xhr.responseText);
                    var searchResultsList = document.getElementById('searchResultsList');
                    products.forEach(function (product) {
                        var productItem = document.createElement('div');
                        productItem.className = 'product-item';
                        productItem.innerHTML = `
                            <a href="productmessage?id=${product.id}&userId=${userId}">
                                <img src="${product.iconUrl}" alt="${product.name}">
                                <h3>${product.name}</h3>
                            </a>
                            <p>￥${product.price}</p>
                        `;
                        searchResultsList.appendChild(productItem);
                    });
                }
            };
            xhr.send();
        }
    };
</script>

</body>
</html>
